package com.example.mybastiplus02.controller;

import com.example.mybastiplus02.bean.Student;
import com.example.mybastiplus02.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {


    @Autowired
    StudentService studentService;

    /**
     *获取所有用户信息
     *URL_ADDRESS: http://localhost:8081/user
     *
     */
    @GetMapping("/Students")
//    @CrossOrigin(origins = "http://localhost:8080")
    public List<Student> getAllStudents(){
        return  studentService.list();
    }


    /**
     * 根据ID查询用户信息
     * URL_ADDRESS: http://localhost:8081/user/{id}
     * @return
     */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable("id") Integer id){
        return studentService.getById(id);
    }

    @PostMapping("/student")
    public String addStudent(@RequestBody Student student){
        if(studentService.save(student)){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }
    @PutMapping("/student")
    public void updateStudent(@RequestBody Student student){
        studentService.updateById(student);
    }

    @DeleteMapping ("/student")
    public void deleteStudent(Integer id){
        studentService.removeById(id);
    }
}